<template>
<div>
  <!------------------------面包屑--------------------------->
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
    <el-breadcrumb-item :to="{path:'/blank'}"><i class="el-icon-help"></i>考试管理</el-breadcrumb-item>
    <el-breadcrumb-item :to="{path:'/bank'}">题库管理</el-breadcrumb-item>
    <el-breadcrumb-item>添加题库</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>

  <!------------------------添加题库------------------------>
  <el-card style="margin: 0 auto;width: 500px">
    <h1 style="font-size: 20px">添加题库</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="margin-top: 20px">
      <el-form-item label="题库名称" prop="bankName">
        <el-input v-model="ruleForm.bankName" placeholder="请输入题库"></el-input>
      </el-form-item>
      <el-form-item label="选择专业" prop="majorId" style="text-align: left">
        <el-select   v-show="localUser.roleId!=3" v-model="ruleForm.majorId" placeholder="请选择专业" >
          <el-option v-for="item in majorList" :key="item.id" :label="item.majorName" :value="item.id"></el-option>
        </el-select>
        <el-select v-show="localUser.roleId==3" v-model="localUser.majorName" placeholder="请选择专业" :disabled="true">
          <el-option v-for="item in majorList" :key="item.id" :label="item.majorName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="float: left">
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="$router.push('/bank')">返回列表</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</div>
</template>

<script>
let localJwt = localStorage.getItem('localJwt');
export default {
  data() {
    return {
      isMajorId:false,
      majorList:[],
      ruleForm: {
        bankName: '',
        majorId: '',
      },
      localUser:{},
      rules: {
        bankName: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        majorId: [
          {required: true, message: '请选择专业', trigger: 'blur'},
        ]
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:9282/bank/add-new";
          console.log('url = '+url);
          let formData = this.qs.stringify(this.ruleForm)
          console.log('formData = ' + formData);

          this.axios
              .create({
                'headers': {
                  'Authorization': localJwt
                }
              })
              .post(url,formData).then((response) =>{
            let jsonResult = response.data;
            const h = this.$createElement;

            if (jsonResult.state == 20000){
              this.$notify({
                title: '添加成功',
                message: h('i', { style: 'color: teal'}, jsonResult.message),
                type: 'success'
              });
              this.resetForm(formName);
              if (jsonResult.jwt!=null){
                localStorage.setItem("localJwt",jsonResult.jwt);
              }
            }else if(jsonResult.state==70100){
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
              this.$router.push('/')
            } else {
              this.$notify.error({
                title: '添加失败',
                message: h('i', { style: 'color: red'}, jsonResult.message),
              });
            }
          })
          console.log('添加完毕！')
        } else {
          console.log('添加失败!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadMajorList(){


    //http://localhost:9282/bank/listMajor
      let url = 'http://localhost:9282/bank/listMajor';
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) =>{
        let jsonResult = response.data;
        console.log(jsonResult);
        if (jsonResult.state == 20000){
          this.majorList = jsonResult.data;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确认',
            callback: action => {// eslint-disable-line no-unused-vars
              this.$router.back();
            }
          })
        }
      });

    },
    loadUser(){
      this.localUser = JSON.parse(localStorage.getItem("localUser"));
      console.log(this.localUser)
      if(this.localUser.roleId==3){
        this.ruleForm.majorId = this.localUser.majorId;
      }

    }
  },
  mounted() {
    this.loadUser();
    this.loadMajorList();
  }
}
</script>

<style scoped>

</style>
